import React from 'react';
import PropTypes from 'prop-types';
import {Provider} from 'react-redux';
import {bindActionCreators} from 'redux';
import {actions, store} from '@redux';

export default class App extends React.Component {

  componentDidMount() {
    const {
      connectSocket,
      sendMessage
    } = actions.ws;
    store.dispatch(connectSocket());
    let count = 1;
    setInterval(() => {
      store.dispatch(sendMessage({count: count++}));
    }, 3000)
  }

  static childContextTypes = {
    actions: PropTypes.object
  };

  getChildContext() {
    const boundActions = {};
    for (let key in actions) {
      if (actions.hasOwnProperty(key)) {
        boundActions[key] = bindActionCreators(actions[key], store.dispatch);
      }
    }
    return {
      actions: boundActions
    };
  }

  render() {
    return (
        <Provider store={store}>
          <div id="root">
            {this.props.children}
          </div>
        </Provider>
    );
  }
}
